Ext.apply(Ext.form.VTypes, {
    pdffiletype : function(val, field) {
		var ext = val.substring(val.length - 3);
		var ok = (ext == 'pdf' || ext == 'PDF' || ext == 'zip' || ext == 'rar');
		var validDOC = Ext.getCmp('file_doc').getValue() != '';
		return (ok || validDOC);
    },

    pdffiletypeText : msg_pdf_type,
	
	dlfiletype : function(val, field) {
		var ext = val.substring(val.length - 3);
		var ok = (ext == 'doc' || ext == 'tex' || ext == 'zip' || ext == 'rar' || ext == 'lat');
		ext = val.substring(val.length - 4);
		ok = (ok || ext == 'docx');
		var validPDF = Ext.getCmp('file_pdf').getValue() != '';
		return (ok || validPDF);
    },

    dlfiletypeText : msg_doc_type
});


Ext.onReady(function(){
	Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

	var paper_id = Ext.urlDecode(window.location.search.substring(1))['id'];
	if(!paper_id){
		paper_id = -1;
	}
	var helper = '';
	if(paper_id != -1){
		helper = '../';
	}
	var p_store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../getpaper'
        }),
        reader: new Ext.data.JsonReader({
            root: 'papers',
            totalProperty: 'totalCount',
            id: 'id'
        }, [
        	{name: 'id'},
            {name: 'title'},
            {name: 'authors'},
            {name: 'abstract'},
            {name: 'area_id'},
			{name: 'category_id'},
			{name: 'email'}
        ])
    });

    var a_store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: helper + '../area/all'
        }),
        reader: new Ext.data.JsonReader({
            root: 'areas',
            totalProperty: 'totalCount',
            id: 'id'
        }, [
        	{name: 'id'},
            {name: 'name_en'},
            {name: 'name_mk'},
            {name: 'short'},
            {name: 'description'}            
        ])
    });
		
    var c_store = new Ext.data.Store({
		remoteSort: true,
		sortInfo: { field: 'name_en', direction: 'ASC'},
        proxy: new Ext.data.HttpProxy({
            url: helper + '../category/byArea'
        }),
        reader: new Ext.data.JsonReader({
            root: 'categories',
            totalProperty: 'totalCount',
            id: 'id'
        }, [
        	{name: 'id'},
            {name: 'name_mk'},            
            {name: 'name_en'},
            {name: 'description', mapping: 'description'}            
        ])
    });
	var area_short;
    
    var c_combo = new Ext.form.ComboBox({
                fieldLabel: lbl_topic,
                hiddenName:'category_id',
                store: c_store,
                valueField:'id',
                displayField:'name_' + lang,
                typeAhead: true,
                mode: 'local',
                emptyText: lbl_select_topic,
				editable: false,
                selectOnFocus:true,
                triggerAction: 'all',
				forseSelection:true,
                allowBlank:false,
                blankText: msg_blank,
                width:190
            });
		
    var uploadForm = new Ext.FormPanel({
        url: helper + 'index',
		id: 'uploadForm',
		fileUpload:true,
        frame:true,
        title: lbl_panel_title,
        bodyStyle:'padding:5px 5px 0',
        labelAlign: 'right',        
        width: 550,        
        waitMsgTarget: true,

        items: [{
			xtype: 'fieldset',
            id: 'fsLogin',
			title: lbl_form_title,
			defaults: {width: 300},
        	defaultType: 'textfield',
			height: 400,
			width: 520,
			labelWidth: 120,
			items:[
				{
                fieldLabel: lbl_title,
                name: 'title',
				blankText: msg_blank,
                allowBlank:false
            },new Ext.form.TextArea({
                fieldLabel: lbl_authors,
				id: 'authors',
                name: 'authors',
				blankText: msg_blank,
                allowBlank:false
            }),new Ext.form.TextArea({
                fieldLabel: lbl_abstract,
                name: 'abstract',
				blankText: msg_blank,
                allowBlank:false
            }),new Ext.form.ComboBox({
                fieldLabel: lbl_area,
                hiddenName:'area_id',
                store: a_store,
                valueField:'id',
                displayField:'name_' + lang,
                typeAhead: true,
                mode: 'local',
                triggerAction: 'all',
                emptyText: lbl_select_area,
                selectOnFocus:true,
				forseSelection:true,
				editable: false,
                allowBlank:false,
                blankText: msg_blank,
                width:190,
				onSelect: function(record){
					c_store.load({params: { area_id : record.data.id}});
					c_combo.setValue('');
					area_short = record.data['short'];
					this.setValue(record.data[this.valueField || this.displayField]);
            		this.collapse();
				}
            }),c_combo,{
                fieldLabel: lbl_pdf,
				id: 'file_pdf',
                name: 'file_pdf',
				inputType: 'file',
				vtype: 'pdffiletype'
            },{
                fieldLabel: lbl_doc,
				id: 'file_doc',
                name: 'file_doc',
				vtype: 'dlfiletype',
				inputType: 'file'
            },{
                fieldLabel: lbl_corespondence,
				id: 'email',
                name: 'email',
				blankText: msg_blank,
				vtype: 'email',
                allowBlank:false
            }
        ],

        buttons: [{
            text: lbl_button,
            type: 'submit',
            name: 'regsbmt',
            handler: function(){
            	var form = uploadForm.getForm();
                var fd = Ext.getCmp('file_doc').getValue();
                var fp = Ext.getCmp('file_pdf').getValue();
                if(fd == '' && fp == ''){
                    Ext.Msg.alert(alertName, alertMsg);        
                    return;
                }
            	if(form.isValid()){
            		form.submit({params:{'area':area_short, 'paper_id': paper_id}, url:helper + 'index', waitMsg: msg_wait, success: regComplete});
            	}
            }
        }]
		}]
    });
	var areaLoaded = function(r, options, success){
		if(paper_id != -1){                        
			p_store.load({params: {id: paper_id}, callback: loaded_paper});	
		}
	}
	var regComplete = function(){
        window.location = helper + 'posted';
	}
	a_store.load({callback: areaLoaded});	
    uploadForm.render('upload-form');
	document.getElementById('email').value = email;
	document.getElementById('authors').value = name;
	var loaded_paper = function(r, options, success){
		var paper = r[0];
        var a_id = paper.get('area_id');
        area_short = a_store.getById(a_id).data.short;
		c_store.load({params: { area_id : a_id}, callback: function(r, options, success){														
			uploadForm.getForm().loadRecord(paper);
			//	c_combo.setValue(c_id);}
		}});
		
	}	
});
